﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KOPLibrary.PreferenceManagement;
using System.Net;

namespace KOPLibrary.Controls
{


    public partial class SmtpSettings : UserControl
    {
        public SmtpSettings()
        {
            InitializeComponent();
        }

        private void ctlSaveSmtpSettings_Click(object sender, EventArgs e)
        {
            PreferenceManager.SaveObject("SmtpSettings", TemplateManagement.TemplateType.Excel, Settings);
        }

        public SmtpSettingsInfo Settings
        {
            get
            {
                SmtpSettingsInfo result = new SmtpSettingsInfo()
                {
                    FromAddress = ctlEMail.Text,
                    FromName = ctlName.Text,
                    Password = ctlPassword.Text,
                    Port = Convert.ToInt32(ctlPort.Value),
                    SMTPServer = ctlHost.Text,
                    SSL = ctlSSL.Checked,
                    Username = ctlUsername.Text
                };
                return result;
            }

            set
            {
                if (value != null)
                {
                    ctlEMail.Text = value.FromAddress;
                    ctlName.Text = value.FromName;
                    ctlPassword.Text = value.Password;
                    ctlPort.Value = value.Port;
                    ctlHost.Text = value.SMTPServer;
                    ctlSSL.Checked = value.SSL;
                    ctlUsername.Text = value.Username;
                }
            }
        }

        internal void LoadSettings()
        {
            object previous = PreferenceManager.ReadObject("SmtpSettings", TemplateManagement.TemplateType.Excel);
            if (previous != null)
            {
                Settings = (SmtpSettingsInfo)previous;
            }
            else Settings = SmtpSettingsInfo.Empty;
        }

        internal void SetClient(System.Net.Mail.SmtpClient client)
        {
            SmtpSettingsInfo settings = Settings;
            client.Host = settings.SMTPServer;
            client.Port = settings.Port;
            client.Credentials = new NetworkCredential(settings.Username, settings.Password);
            client.EnableSsl = settings.SSL;            
        }

        private void ctlQuickSet_Click(object sender, EventArgs e)
        {
            Point p = this.Parent.PointToScreen(new Point(ctlQuickSet.Left, ctlQuickSet.Top + ctlQuickSet.Height));
            ctlQuickSetMenu.Show(p);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void hotmailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ctlHost.Text = "smtp.live.com";
            ctlPort.Value = 25;
            ctlSSL.Checked = true;
        }
    }

    [Serializable]
    public class SmtpSettingsInfo
    {
        public string FromName { get; set; }
        public string FromAddress { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string SMTPServer { get; set; }
        public int Port { get; set; }
        public bool SSL { get; set; }

        public static SmtpSettingsInfo Empty
        {
            get
            {
                SmtpSettingsInfo info = new SmtpSettingsInfo() { Port = 25 };
                return info;
            }
        }
    }
}
